package com.ruoyi.userInfo.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.userInfo.domain.IaeRegions;
import com.ruoyi.userInfo.mapper.IaeRegionsMapper;
import com.ruoyi.userInfo.service.IaeRegionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Author: Z.H.Z
 * Date: 2025/2/6 15:01
 */
@Service
public class IaeRegionsServiceImpl implements IaeRegionsService {

    @Autowired
    private IaeRegionsMapper iaeRegionsMapper;


    @Override
    public AjaxResult listDto() {
        List<IaeRegions> selectList = iaeRegionsMapper.selectRegionsLists(Wrappers.emptyWrapper());
        //启动流线程
        List<IaeRegions> collect = selectList.stream()
                .filter(regions -> regions.getParentId()==0)
                .map((regionsList -> {
                    IaeRegions iaeRegions = new IaeRegions();
                    iaeRegions.setId(regionsList.getId());
                    iaeRegions.setName(regionsList.getName());
                    iaeRegions.setChildren(getChildren(selectList,regionsList.getId()));
                    return iaeRegions;
                })).collect(Collectors.toList());
        return AjaxResult.success("200",collect);
    }

    private List<IaeRegions> getChildren(List<IaeRegions> selectList, Integer id) {


        //启动流线程
        List<IaeRegions> collect = selectList.stream()
                .filter(typeDTO -> typeDTO.getParentId().equals(id))
                .map((selectListDto -> {
                    IaeRegions information = new IaeRegions();
                    information.setId(selectListDto.getId());
                    information.setName(selectListDto.getName());
                    information.setChildren(getChildren(selectList,selectListDto.getId()));
                    return information;
                })).collect(Collectors.toList());
        return collect.size()==0?null:collect;

    }
}
